package com.wh.accesscontrol.task;


import com.wh.accesscontrol.entity.Stadium;
import com.wh.accesscontrol.entity.Total;
import com.wh.accesscontrol.service.lmpl.StadiumServiceImpl;
import com.wh.accesscontrol.service.lmpl.StudentServiceImpl;
import com.wh.accesscontrol.service.lmpl.TotalServiceImpl;
import com.wh.accesscontrol.util.DateUtil;
import com.wh.accesscontrol.util.LogUtil;
import com.wh.accesscontrol.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Component
@EnableScheduling//启动定时任务
public class TimeTask {
    /**
     * <h3>accesscontrol</h3>
     * <p>模拟进出人员定时任务</p>
     *
     * @author : WH
     * @date : 2020-03-09 20:12
     **/
    @Autowired
    private StadiumServiceImpl stadiumService;
    @Autowired
    private StudentServiceImpl studentService;
    @Autowired
    private TotalServiceImpl totalService;

    public  final  Logger logger = Logger.getLogger(String.valueOf(TimeTask.class));

    //@Scheduled(cron = "0 0/30 0,23 * * *")   //每天23：30执行定时任务
    @Scheduled(cron = "0 0/1 * * * *")   //每天23：30执行定时任务


    public void run() {
        Date date = new Date();
        long a = System.currentTimeMillis();
        System.out.println("定时任务开始" + date);
        //统计一天所有通行记录

        String info=date+"---->定时任务开始";
        String error="";

        try {
            AllDaySum();
        } catch (Exception e) {
            logger.info("ceshi ");
            info+="天历史统计数据异常";
            error+="\n 天历史记录统计失败！\n"+e.getMessage();
            e.printStackTrace();
        }
        long b = System.currentTimeMillis();
        long time1=b-a;

        info+="\n天历史统计成功！"+time1+"ms \n";



        try {
            if (DateUtil.isLastDayOfMonth(date))
            AllMonthSum();
        } catch (Exception e) {
            info+="月历史统计数据异常";
            error+="\n 月历史记录统计失败！\n"+e.getMessage();
            e.printStackTrace();
        }
        long d = System.currentTimeMillis();
        long time2 = d - b;
        info+="月历史统计成功！"+time2+"ms \n";



        try {
            if (DateUtil.isLastDayOfYear(date))
            AllYearSum();
        } catch (Exception e) {
            info+="年历史统计数据异常";
            error+="\n 年历史记录统计失败！\n"+e.getMessage();
            e.printStackTrace();
        }
        long e = System.currentTimeMillis();
        long time3 = e - d;
        info+="年历史统计成功！"+time3+"ms \n";
        LogUtil.input("定时任务",info);
        if (!error.equals(""))
            LogUtil.input("定时任务——error",error);

    }

    /**
     * 统计一天所有通行记录
     */
    private void AllDaySum() {
        System.out.println("日记录统计执行");
        List<Stadium> stadiums = new ArrayList<>();
        stadiums = stadiumService.listByAllday();
        ArrayList<Integer> studentid = (ArrayList<Integer>) studentService.AllStudentid();
        long bakid = 0;
        int schoolgate = 0;
        int classroom = 0;
        int library = 0;
        int gymnasium = 0;
        int dorm = 0;
        Total total = new Total();
        Total total1 = new Total();
        Total total2 = new Total();
        Total total3 = new Total();
        Total total4 = new Total();
        for (Stadium s : stadiums) {
            if (bakid == 0) {
                bakid = s.getStudentid();
            }
            if (bakid != s.getStudentid()) {

                if (total.getStudentid() != null) {
                    totalService.add(total);
                }
                if (total1.getStudentid() != null) {
                    totalService.add(total1);
                }
                if (total2.getStudentid() != null) {
                    totalService.add(total2);
                }
                if (total3.getStudentid() != null) {
                    totalService.add(total3);
                }
                if (total4.getStudentid() != null) {
                    totalService.add(total4);
                }
                schoolgate = 0;
                classroom = 0;
                library = 0;
                gymnasium = 0;
                dorm = 0;
                bakid = s.getStudentid();
            }
            //最后一次循环
            if (stadiums.size() - 1 == stadiums.indexOf(s)) {
                String type = String.valueOf(s.getType());

                if (type.equals("10")) {
                    total.setStudentid(s.getStudentid().intValue());
                    total.setType(MathUtil.parseInt(type));
                    schoolgate++;
                    total.setSum(schoolgate);
                    total.setStadiumid(s.getStadiumid());
                    total.setUnit(1);
                    total.setTime(new Date());

                }
                if (type.substring(0, 1).equals("1")) {
                    total1.setStudentid(s.getStudentid().intValue());
                    total1.setType(MathUtil.parseInt(type));
                    classroom++;
                    total1.setSum(classroom);
                    total1.setStadiumid(s.getStadiumid());
                    total1.setUnit(1);
                    total1.setTime(new Date());
                }
                if (type.substring(0, 1).equals("2")) {
                    total2.setStudentid(s.getStudentid().intValue());
                    total2.setType(MathUtil.parseInt(type));
                    library++;
                    total2.setSum(library);
                    total2.setStadiumid(s.getStadiumid());
                    total2.setUnit(1);
                    total2.setTime(new Date());
                }
                if (type.substring(0, 1).equals("3")) {
                    total3.setStudentid(s.getStudentid().intValue());
                    total3.setType(MathUtil.parseInt(type));
                    gymnasium++;
                    total3.setSum(gymnasium);
                    total3.setStadiumid(s.getStadiumid());
                    total3.setUnit(1);
                    total3.setTime(new Date());
                }

                if (type.substring(0, 1).equals("4")) {

                    total4.setStudentid(s.getStudentid().intValue());
                    total4.setType(MathUtil.parseInt(type));
                    dorm++;
                    total4.setSum(dorm);
                    total4.setStadiumid(s.getStadiumid());
                    total4.setUnit(1);
                    total4.setTime(new Date());

                }

                if (total.getStudentid() != null) {

                    totalService.add(total);
                }
                if (total1.getStudentid() != null) {
                    totalService.add(total1);
                }
                if (total2.getStudentid() != null) {
                    totalService.add(total2);
                }
                if (total3.getStudentid() != null) {
                    totalService.add(total3);
                }
                if (total4.getStudentid() != null) {
                    totalService.add(total4);
                }
            }
            for (Integer id : studentid) {
                if ((long) id == s.getStudentid()
                ) {
                    String type = String.valueOf(s.getType());

                    if (type.equals("10")) {
                        total.setStudentid(id);
                        total.setType(MathUtil.parseInt(type));
                        schoolgate++;
                        total.setSum(schoolgate);
                        total.setStadiumid(s.getStadiumid());
                        total.setUnit(1);
                        total.setTime(new Date());

                        continue;
                    }
                    if (type.substring(0, 1).equals("1")) {
                        total1.setStudentid(id);
                        total1.setType(MathUtil.parseInt(type));
                        classroom++;
                        total1.setSum(classroom);
                        total1.setStadiumid(s.getStadiumid());
                        total1.setUnit(1);
                        total1.setTime(new Date());

                    }
                    if (type.substring(0, 1).equals("2")) {
                        total2.setStudentid(id);
                        total2.setType(MathUtil.parseInt(type));
                        library++;
                        total2.setSum(library);
                        total2.setStadiumid(s.getStadiumid());
                        total2.setUnit(1);
                        total2.setTime(new Date());

                    }
                    if (type.substring(0, 1).equals("3")) {
                        total3.setStudentid(id);
                        total3.setType(MathUtil.parseInt(type));
                        gymnasium++;
                        total3.setSum(gymnasium);
                        total3.setStadiumid(s.getStadiumid());
                        total3.setUnit(1);
                        total3.setTime(new Date());
                    }

                    if (type.substring(0, 1).equals("4")) {
                        total4.setStudentid(id);
                        total4.setType(MathUtil.parseInt(type));
                        dorm++;
                        total4.setSum(dorm);
                        total4.setStadiumid(s.getStadiumid());
                        total4.setUnit(1);
                        total4.setTime(new Date());
                    }
                }
            }
        }
    }

    /**
     * 整个月总和
     */
    private void AllMonthSum() {
        System.out.println("月记录统计执行");
        List<Total> totals = new ArrayList<>();
        totals = totalService.selectAllDay(1);
        ArrayList<Integer> studentid = (ArrayList<Integer>) studentService.AllStudentid();
        long bakid = 0;
        int schoolgate = 0;
        int classroom = 0;
        int library = 0;
        int gymnasium = 0;
        int dorm = 0;
        Total total = new Total();
        Total total1 = new Total();
        Total total2 = new Total();
        Total total3 = new Total();
        Total total4 = new Total();
        for (Total t : totals) {

            if (bakid == 0) {
                bakid = t.getStudentid();
            }
            if (bakid != t.getStudentid()) {

                if (total.getStudentid() != null) {
                    totalService.add(total);
                }
                if (total1.getStudentid() != null) {
                    totalService.add(total1);
                }
                if (total2.getStudentid() != null) {
                    totalService.add(total2);
                }
                if (total3.getStudentid() != null) {
                    totalService.add(total3);
                }
                if (total4.getStudentid() != null) {
                    totalService.add(total4);
                }
                schoolgate = 0;
                classroom = 0;
                library = 0;
                gymnasium = 0;
                dorm = 0;
                bakid = t.getStudentid();
            }
            //最后一次循环
            if (totals.size() - 1 == totals.indexOf(t)) {
                String type = String.valueOf(t.getType());

                if (type.equals("10")) {
                    total.setStudentid(t.getStudentid().intValue());
                    total.setType(MathUtil.parseInt(type));
                    schoolgate += t.getSum();
                    total.setSum(schoolgate);
                    total.setStadiumid(t.getStadiumid());
                    total.setUnit(2);
                    total.setTime(new Date());

                }
                if (type.substring(0, 1).equals("1")) {
                    total1.setStudentid(t.getStudentid().intValue());
                    total1.setType(MathUtil.parseInt(type));
                    classroom += t.getSum();
                    total1.setSum(classroom);
                    total1.setStadiumid(t.getStadiumid());
                    total1.setUnit(2);
                    total1.setTime(new Date());
                }
                if (type.substring(0, 1).equals("2")) {
                    total2.setStudentid(t.getStudentid().intValue());
                    total2.setType(MathUtil.parseInt(type));
                    library += t.getSum();
                    total2.setSum(library);
                    total2.setStadiumid(t.getStadiumid());
                    total2.setUnit(2);
                    total2.setTime(new Date());
                }
                if (type.substring(0, 1).equals("3")) {
                    total3.setStudentid(t.getStudentid().intValue());
                    total3.setType(MathUtil.parseInt(type));
                    gymnasium += t.getSum();
                    total3.setSum(gymnasium);
                    total3.setStadiumid(t.getStadiumid());
                    total3.setUnit(2);
                    total3.setTime(new Date());
                }

                if (type.substring(0, 1).equals("4")) {

                    total4.setStudentid(t.getStudentid().intValue());
                    total4.setType(MathUtil.parseInt(type));
                    dorm++;
                    total4.setSum(dorm);
                    total4.setStadiumid(t.getStadiumid());
                    total4.setUnit(2);
                    total4.setTime(new Date());

                }

                if (total.getStudentid() != null) {

                    totalService.add(total);
                }
                if (total1.getStudentid() != null) {
                    totalService.add(total1);

                }
                if (total2.getStudentid() != null) {
                    totalService.add(total2);
                }
                if (total3.getStudentid() != null) {
                    totalService.add(total3);
                }
                if (total4.getStudentid() != null) {
                    totalService.add(total4);
                }
            }
            for (int id : studentid) {

                if ((long) id == t.getStudentid()
                ) {
                    String type = String.valueOf(t.getType());

                    if (type.equals("10")) {
                        total.setStudentid(id);
                        total.setType(MathUtil.parseInt(type));
                        schoolgate += t.getSum();
                        total.setSum(schoolgate);
                        total.setStadiumid(t.getStadiumid());
                        total.setUnit(2);
                        total.setTime(new Date());

                        continue;
                    }
                    if (type.substring(0, 1).equals("1")) {
                        total1.setStudentid(id);
                        total1.setType(MathUtil.parseInt(type));
                        classroom += t.getSum();
                        total1.setSum(classroom);
                        total1.setStadiumid(t.getStadiumid());
                        total1.setUnit(2);
                        total1.setTime(new Date());

                    }
                    if (type.substring(0, 1).equals("2")) {
                        total2.setStudentid(id);
                        total2.setType(MathUtil.parseInt(type));
                        library += t.getSum();
                        total2.setSum(library);
                        total2.setStadiumid(t.getStadiumid());
                        total2.setUnit(2);
                        total2.setTime(new Date());

                    }
                    if (type.substring(0, 1).equals("3")) {
                        total3.setStudentid(id);
                        total3.setType(MathUtil.parseInt(type));
                        gymnasium += t.getSum();
                        total3.setSum(gymnasium);
                        total3.setStadiumid(t.getStadiumid());
                        total3.setUnit(2);
                        total3.setTime(new Date());
                    }

                    if (type.substring(0, 1).equals("4")) {
                        total4.setStudentid(id);
                        total4.setType(MathUtil.parseInt(type));
                        dorm += t.getSum();
                        total4.setSum(dorm);
                        total4.setStadiumid(t.getStadiumid());
                        total4.setUnit(2);
                        total4.setTime(new Date());
                    }
                }

            }

        }
    }

    /**
     * 全年总和
     */
    private void AllYearSum() {
        System.out.println("年记录统计执行");
        List<Total> totals = new ArrayList<>();
        totals = totalService.selectAllDay(2);
        ArrayList<Integer> studentid = (ArrayList<Integer>) studentService.AllStudentid();
        long bakid = 0;
        int schoolgate = 0;
        int classroom = 0;
        int library = 0;
        int gymnasium = 0;
        int dorm = 0;
        Total total = new Total();
        Total total1 = new Total();
        Total total2 = new Total();
        Total total3 = new Total();
        Total total4 = new Total();
        for (Total t : totals) {

            if (bakid == 0) {
                bakid = t.getStudentid();
            }
            if (bakid != t.getStudentid()) {

                if (total.getStudentid() != null) {
                    totalService.add(total);
                }
                if (total1.getStudentid() != null) {
                    totalService.add(total1);
                }
                if (total2.getStudentid() != null) {
                    totalService.add(total2);
                }
                if (total3.getStudentid() != null) {
                    totalService.add(total3);
                }
                if (total4.getStudentid() != null) {
                    totalService.add(total4);
                }
                schoolgate = 0;
                classroom = 0;
                library = 0;
                gymnasium = 0;
                dorm = 0;
                bakid = t.getStudentid();
            }
            //最后一次循环
            if (totals.size() - 1 == totals.indexOf(t)) {
                String type = String.valueOf(t.getType());

                if (type.equals("10")) {
                    total.setStudentid(t.getStudentid().intValue());
                    total.setType(MathUtil.parseInt(type));
                    schoolgate += t.getSum();
                    total.setSum(schoolgate);
                    total.setStadiumid(t.getStadiumid());
                    total.setUnit(3);
                    total.setTime(new Date());

                }
                if (type.substring(0, 1).equals("1")) {
                    total1.setStudentid(t.getStudentid().intValue());
                    total1.setType(MathUtil.parseInt(type));
                    classroom += t.getSum();
                    total1.setSum(classroom);
                    total1.setStadiumid(t.getStadiumid());
                    total1.setUnit(3);
                    total1.setTime(new Date());
                }
                if (type.substring(0, 1).equals("2")) {
                    total2.setStudentid(t.getStudentid().intValue());
                    total2.setType(MathUtil.parseInt(type));
                    library += t.getSum();
                    total2.setSum(library);
                    total2.setStadiumid(t.getStadiumid());
                    total2.setUnit(3);
                    total2.setTime(new Date());
                }
                if (type.substring(0, 1).equals("3")) {
                    total3.setStudentid(t.getStudentid().intValue());
                    total3.setType(MathUtil.parseInt(type));
                    gymnasium += t.getSum();
                    total3.setSum(gymnasium);
                    total3.setStadiumid(t.getStadiumid());
                    total3.setUnit(3);
                    total3.setTime(new Date());
                }

                if (type.substring(0, 1).equals("4")) {

                    total4.setStudentid(t.getStudentid().intValue());
                    total4.setType(MathUtil.parseInt(type));
                    dorm++;
                    total4.setSum(dorm);
                    total4.setStadiumid(t.getStadiumid());
                    total4.setUnit(3);
                    total4.setTime(new Date());

                }

                if (total.getStudentid() != null) {

                    totalService.add(total);
                }
                if (total1.getStudentid() != null) {
                    totalService.add(total1);

                }
                if (total2.getStudentid() != null) {
                    totalService.add(total2);
                }
                if (total3.getStudentid() != null) {
                    totalService.add(total3);
                }
                if (total4.getStudentid() != null) {
                    totalService.add(total4);
                }
            }
            for (int id : studentid) {

                if ((long) id == t.getStudentid()
                ) {
                    String type = String.valueOf(t.getType());

                    if (type.equals("10")) {
                        total.setStudentid(id);
                        total.setType(MathUtil.parseInt(type));
                        schoolgate += t.getSum();
                        total.setSum(schoolgate);
                        total.setStadiumid(t.getStadiumid());
                        total.setUnit(3);
                        total.setTime(new Date());

                        continue;
                    }
                    if (type.substring(0, 1).equals("1")) {
                        total1.setStudentid(id);
                        total1.setType(MathUtil.parseInt(type));
                        classroom += t.getSum();
                        total1.setSum(classroom);
                        total1.setStadiumid(t.getStadiumid());
                        total1.setUnit(3);
                        total1.setTime(new Date());

                    }
                    if (type.substring(0, 1).equals("2")) {
                        total2.setStudentid(id);
                        total2.setType(MathUtil.parseInt(type));
                        library += t.getSum();
                        total2.setSum(library);
                        total2.setStadiumid(t.getStadiumid());
                        total2.setUnit(3);
                        total2.setTime(new Date());

                    }
                    if (type.substring(0, 1).equals("3")) {
                        total3.setStudentid(id);
                        total3.setType(MathUtil.parseInt(type));
                        gymnasium += t.getSum();
                        total3.setSum(gymnasium);
                        total3.setStadiumid(t.getStadiumid());
                        total3.setUnit(3);
                        total3.setTime(new Date());
                    }

                    if (type.substring(0, 1).equals("4")) {
                        total4.setStudentid(id);
                        total4.setType(MathUtil.parseInt(type));
                        dorm += t.getSum();
                        total4.setSum(dorm);
                        total4.setStadiumid(t.getStadiumid());
                        total4.setUnit(3);
                        total4.setTime(new Date());
                    }
                }

            }

        }
    }


}
